package com.clinic.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

public class Util_imp {

	public static SessionFactory get_sessionfactory() {
		Configuration config = new Configuration();
		config.configure();
		ServiceRegistry serviceregistry = new StandardServiceRegistryBuilder()
				.applySettings(config.getProperties()).build();
		return new Configuration().configure().buildSessionFactory(
				serviceregistry);
	}

	public static String to_json(Object object) {
		ObjectWriter ow = new ObjectMapper().writer()
				.withDefaultPrettyPrinter();
		try {
			return ow.writeValueAsString(object);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String to_json(List<Object> loo) {
		ObjectWriter ow = new ObjectMapper().writer()
				.withDefaultPrettyPrinter();
		try {
			return ow.writeValueAsString(loo);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Object from_json(InputStream jsonobject, Object classname) {
		System.out.println("Enter - from_json Util");
		ObjectMapper mapper = new ObjectMapper();
		try {
			System.out.println("Exit - from_json Util");
			return mapper.readValue(jsonobject, classname.getClass());
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("Exit - from_json Util");
		return null;
	}
	
	public static Object from_json(String jsonobject, Object classname) {
		System.out.println("Enter - from_json Util");
		ObjectMapper mapper = new ObjectMapper();
		try {
			System.out.println("Exit - from_json Util");
			return mapper.readValue(jsonobject, classname.getClass());
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("Exit - from_json Util");
		return null;
	}
}